﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Devart.Data.Oracle;

namespace DbSharp.Tools.DbConnectionBuilder.Core.Plugin
{
    /// <summary>
    /// Use Devart Oracle provider to replace the MS Oracle provider as the latter is obsolete
    /// </summary>
    class OracleConnectionPlugin2 : IDbConnectionPlugin
    {

        #region IDbConnectionPlugin Members

        public PluginNamePair PluginNamePair
        {
            get { return new PluginNamePair("Devart.Data.Oracle") { DisplayName = "Oracle Devart Driver" }; }
        }

        public DbConnectionStringBuilder CreateConnectionStrBuilder(DbServerCredential credential)
        {
            var builder = new OracleConnectionStringBuilder();
            builder.ConnectMode = OracleConnectMode.Default;
            builder.Server = credential.DataSource;
            builder.UserId = credential.UserId;
            builder.Password = credential.Password;
            builder.Unicode = true;

            return builder;
        }

        public DbServerCredential CreateCredential(string connectionString)
        {
            DbServerCredential credential = new DbServerCredential();
            credential.UserIdEnabled = true;
            credential.PasswordEnabled = true;
            credential.DataSourceEnabled = true;

            var builder = new OracleConnectionStringBuilder(connectionString);
            object userId;
            if (builder.TryGetValue("User ID", out userId))
            {
                credential.UserId = userId.ToString();
            }
            object pwd;
            if (builder.TryGetValue("Password", out pwd))
            {
                credential.Password = pwd.ToString();
            }
            object dataSource;
            if (builder.TryGetValue("Data Source", out dataSource))
            {
                credential.DataSource = dataSource.ToString();
            }

            credential.AdditionalInfo = "";

            return credential;
        }

        public DbConnection CreateDbConnection(string connectionString)
        {
            return new OracleConnection(connectionString);
        }

        public List<FileVersionPair> GetCandidateDbFileVersionPairs()
        {
            var list = new List<FileVersionPair>();
            return list;
        }


        #endregion
    }
}
